<?php

namespace Database\Seeders;

use App\Models\Enterprise;
use App\Models\ESStation;
use App\Models\User;
use Illuminate\Database\Seeder;

class InitUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::factory(8)->hasAttached(
            ESStation::factory()->count(3),
            ['name' => 'test']
        )->create();

        $admin = User::find(1);
        $admin->update([
            'name' => 'admin_admin', 
            'password' => bcrypt('AdminPassGG') ,
            'remarks' => '初始超级账户']
        );
        $admin->assignRole('超级管理员');
        $admin->assignEnterprise('冠盖（上海）科技有限公司');
        
        $users = User::all();
        foreach($users->diff(User::where('id', $admin->id)->get()) as $user){
            $user->assignRole('操作人员');
            $ids = Enterprise::all()->pluck('id')->toArray();
            $id = $ids[array_rand($ids)];
            $user->enterprises()->sync($ids[array_rand($ids)], false);
        }


    }
}
